﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using QLKDS.DTO;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;

namespace QLKDS.Forms
{
    public partial class frmBookStore : Form
    {
        #region Properties
        private BookStoreManager bookStoreManager;
        private StoreManager storeManager = new StoreManager();
        private BarManager barManagerContext;
        private PopupMenu menuContext;

        #endregion


        #region Methods

        public frmBookStore()
        {
            InitializeComponent();

            this.bookStoreManager = new BookStoreManager();
            this.grvBookstore.GroupPanelText = "Kéo một cột vào đây để nhóm dữ liệu";

            this.barManagerContext = new BarManager();
            this.menuContext = new PopupMenu();

            this.menuContext.ItemLinks.Add(new BarButtonItem(this.barManagerContext, "&Thêm mới"));
            this.menuContext.ItemLinks.Add(new BarButtonItem(this.barManagerContext, "&Sửa"));
            this.menuContext.ItemLinks.Add(new BarButtonItem(this.barManagerContext, "&Xóa"));
        }



        private void frmBookStore_Load(object sender, EventArgs e)
        {
            // Population data on bookStore gridview
            this.PopulateBookStoreGridView();

            this.lueStore.Properties.DataSource = this.storeManager.ListStore();
            this.lueStore.Properties.ValueMember = "Id";
            this.lueStore.Properties.DisplayMember = "Name";
        }

        private void frmBookStore_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmMain.MainWindowManager.WindowNames.Remove(this.Name);
            frmMain.MainWindowManager.Windows.Remove(this);
        }

        public void PopulateBookStoreGridView()
        {
            this.Cursor = Cursors.WaitCursor;

            this.gridBookstore.DataSource = this.bookStoreManager.ListBookStoreByStoreId(this.lueStore.Text.Trim());

            this.Cursor = Cursors.Default;
        }

        #endregion



        private void btnAdd_Click(object sender, EventArgs e)
        {

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int row = (int)grvBookstore.FocusedRowHandle;
            if (row >= 0)
            {
                int id = int.Parse((grvBookstore.GetRowCellValue(row, grvBookstore.Columns["Id"]) + "").ToString());

                string name = (grvBookstore.GetRowCellValue(row, grvBookstore.Columns["Name"]) + "").ToString();

                if (XtraMessageBox.Show("Bạn có muốn xóa khách hàng : " + name + " không?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.bookStoreManager.DeleteBookStore(id);
                    this.PopulateBookStoreGridView();
                    XtraMessageBox.Show("Xóa dữ liệu thành công");
                }
            }   
        }

        private void grvBookstore_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
             GridView view = sender as GridView;
             if (!view.IsNewItemRow(e.RowHandle))
             {
                 int row = (int)grvBookstore.FocusedRowHandle;
                 if (row >= 0)
                 {
                     var bookStore = new BookStore();
                     bookStore.Id = int.Parse((grvBookstore.GetRowCellValue(row, grvBookstore.Columns["Id"]) + "").ToString());
                     bookStore.BookId = int.Parse((grvBookstore.GetRowCellValue(row, grvBookstore.Columns["BookId"]) + "").ToString());
                     bookStore.Quantity = int.Parse((grvBookstore.GetRowCellValue(row, grvBookstore.Columns["Quanlity"]) + "").ToString());
                     this.bookStoreManager.UpdateBookStore(bookStore);
                     PopulateBookStoreGridView();
                 }
             }
      
        }

        private void lueStore_EditValueChanged(object sender, EventArgs e)
        {
            this.PopulateBookStoreGridView();
        }

      



     
    }
}
